/**
 * 
 */
package com.jm.web.data;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jm.utils.StringHelper;
import com.jm.web.form.Paging;


/**
 * @author yanjie
 *
 * 2011/10/10 上午9:54:27
 */
public class ReturnData extends BaseDataEntity{
	public static final String KEY_SEARCH_RESULT = "searchResult";
	public static final String KEY_TABLE_ROW = "TABLE_ROW";
	public static final String KEY_TABLE_ROW_INDEX = "TABLE_ROW_INDEX";
	public static final String KEY_TABLE_ROW_LASTRECORD = "TABLE_ROW_LASTRECORD";
	private List tableData = null;
	PageResult callResult = null ;
	
	public PageResult getCallResult() {
		return callResult;
	}

	/**
	 * 
	 */
	public ReturnData() {
		callResult = new PageResult();
		callResult.setRetData(this);
		this.tableData = new ArrayList();
	}
	
	public void Register(String strFormItemId,String strErrorInfo){
		this.callResult.Register(strFormItemId, strErrorInfo);
	}
	
	/**
     * 设置返回值
     * @param value
     */
    public void setRetCode(int value)
    {
    	this.callResult.setRetCode(value);
    }
    
    /**
     * 设置错误信息
     * @param value
     */
    public void setErrorInfo(String value)
    {
    	this.callResult.setErrorInfo(value);
    }
    
    public boolean isError(){
    	return this.callResult.IsError();
    }
    
    
    /**
     * @param strOkInfo
     * 设置OK信息
     */
    public void setOkInfo(String strOkInfo) {
		this.callResult.setOkInfo(strOkInfo);
	}
    
    public void setWarningInfo(String strWarningInfo) {
		this.callResult.setWarningInfo(strWarningInfo);
	}
    
    public void setPromptInfo(String strPromptInfo) {
		this.callResult.setPromptInfo(strPromptInfo);
	}
    
    /**
	 * 设置执行的脚本
	 * @param strJSCode
	 */
	public void setJSCode(String strJSCode)
	{
		this.callResult.setJSCode(strJSCode);
	}
	
	 /**
	 * 设置执行的脚本
	 * @param strJSCode
	 */
	public void setJSBeforeCode(String strJSCode)
	{
		this.callResult.setJSBeforeCode(strJSCode);
	}
	
	/**
	 * 附加JS脚本
	 * @param strJSCode
	 */
	public void AppendJSCode(String strJSCode)
	{
		this.callResult.AppendJSCode(strJSCode);
	}
	
	/**
	 * 附加JS脚本（操作之前）
	 * @param strJSCode
	 */
	public void AppendJSBeforeCode(String strJSBeforeCode)
	{
		this.callResult.AppendJSBeforeCode(strJSBeforeCode);
	}
	
    /**
     * @return 
     * 返回查詢結果
     */
    public Object getSerchResult(){
    	return GetParamValue(KEY_SEARCH_RESULT);
    }
    
    /**
     * @param objParamValue
     * 設置查詢結果
     */
    public void SetSearchResult(Object objParamValue){
    	this.SetParamValue(KEY_SEARCH_RESULT, objParamValue);
    }
    
    @Override
    public String ToJSONString() {
    	return super.ToJSONString();
    }
    
    public Object getTableRow(){
    	return this.getTabledata().GetParamValue(KEY_TABLE_ROW);
    }
    
    public void setTableRow(Object row){
    	this.getTabledata().SetParamValue(KEY_TABLE_ROW, row);
    }
    
    public int getTableRowIndex(){
    	return this.getTabledata().gpiv(KEY_TABLE_ROW_INDEX);
    }
    
    public int getRowNumber(){
    	return this.getTableRowIndex()+1;
    }
    
    public int getTableGroupIndex(String group){
    	return this.getTabledata().gpiv(group);
    }
    
    public boolean isLastRecord(){
    	return this.getTabledata().gpbv(KEY_TABLE_ROW_LASTRECORD,false);
    }
    
    public Map getGroupMap(){
    	return (Map)this.getTabledata().gpov("groupMap");
    }
    
    public void setGroupDisplay(String group,boolean bDis){
    	this.getGroupMap().put(group, bDis);
    }
    
    public boolean isGroupDisplay(){
    	return this.isGroupDisplay("basegroup");
    }
    
    public void setGroupValue(String group,String value){
    	this.getGroupMap().put(group+"_value", value);
    }
    
    //取每一行tr的group值 
    public String getGroupValue(String group){
    	return StringHelper.getValue(this.getGroupMap().get(group+"_value"));
    }
    
  //取每上一行tr的group值 
    public String getPreGroupValue(String group){
    	return StringHelper.getValue(this.getGroupMap().get(group+"_prevalue"));
    }
    
    public void setPreGroupValue(String group,String value){
    	this.getGroupMap().put(group+"_prevalue", value);
    }
    
    public boolean isGroupDisplay(String group){
    	if(this.getGroupMap() == null || !this.getGroupMap().containsKey(group)) return false;
    	return (Boolean)this.getGroupMap().get(group);
    }
    
    public void newTableData(){
    	Map groupMap = new HashMap();;//記錄表格分組合并的情況
    	BaseDataEntity data = new BaseDataEntity();
    	data.spov("groupMap", groupMap);
    	this.tableData.add(data);
    }
    public void removeTableData(){
    	this.tableData.remove(this.tableData.size()-1);
    }
    public BaseDataEntity getTabledata(){
    	return (BaseDataEntity)this.tableData.get(this.tableData.size()-1);
    }
    
    public void setTableData(String key,Object object){
    	this.getTabledata().spov(key, object);
    }
    
	public Object grov(String strParamName)
	{
		String strParamName2 = strParamName.toUpperCase();
		Object row = this.getTableRow();
		if(row == null)return null;
		
		try{
			if(row instanceof ResultSet ){
				return ((ResultSet)row).getObject(strParamName);
			}else if(row instanceof BaseDataEntity){
				return ((BaseDataEntity)row).gpov(strParamName);
			}else if(row instanceof Map ){
				return ((Map)row).get(strParamName);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return null;
	} 
	
	public String grsv(String strParamName){
		return this.grsv(strParamName, "");
	}
	
	public String grsv(String strParamName, String strDefault){
		Object objValue = grov(strParamName);
		if (StringHelper.IsNullOrEmpty(StringHelper.getValue(objValue)))
		{
			return strDefault;
		}
		try
		{
			if(objValue instanceof String[] && ((String[])objValue).length > 0){
				return ((String[])objValue)[0].trim(); 
			}else if(objValue instanceof Date)
				return GetFormatDate(objValue);
			return objValue.toString().trim();
		}
		catch (Exception ex)
		{
			return strDefault;
		}
	}
	public int griv(String strParamName){
		return this.griv(strParamName,0);
	}
	
	public int griv(String strParamName, int nDefault){
		Object objValue = this.grov(strParamName);
		if (StringHelper.IsNullOrEmpty(StringHelper.getValue(objValue)))
		{
			return nDefault;
		}
		try
		{
			return Integer.parseInt(objValue.toString());
		}
		catch (Exception ex)
		{
			return nDefault;
		}
	} 
	public double grdv(String strParamName){
		return this.grdv(strParamName,0.0);
	}
	public double grdv(String strParamName, double fDefault)
	{
		Object objValue = this.grov(strParamName);
		if (StringHelper.IsNullOrEmpty(StringHelper.getValue(objValue)))
		{
			return fDefault;
		}
		try
		{
			return Double.parseDouble(objValue.toString());
		}
		catch (Exception ex)
		{
			return fDefault;
		}
	}
	
	public boolean grbv(String strParamName, boolean bDefault)
	{
		Object objValue = this.grov(strParamName);
		if (StringHelper.IsNullOrEmpty(StringHelper.getValue(objValue)))
		{
			return bDefault;
		}
		try
		{
			return Boolean.parseBoolean(objValue.toString());
		}
		catch (Exception ex)
		{
			return bDefault;
		}
	}
	

	public String transJMStatus(){
		return this.transJMStatus(this.grsv("wfstate"), this.grsv("jmstatus"));
	}
	
	/**
	 * @param gridid
	 * @return
	 */
	public Paging getPaging(String gridid){
		Paging paging = (Paging)this.gpov("paging_"+gridid);
		if(paging == null)paging = new Paging();
		return paging;
	}
}
